package com.zl.user.controller;

import com.zl.basic.exception.BusinessException;
import com.zl.basic.util.AjaxResult;
import com.zl.user.dto.LoginDto;
import com.zl.user.service.LoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginInfoService loginInfoService;

    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginInfoService.loginAccount(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试！！！");

        }
    }

    @GetMapping ("/wechat/{code}")
    public AjaxResult loginWechat(@PathVariable("code") String code){
        try {

            return loginInfoService.loginWechat(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试！！！");

        }
    }


    @PostMapping ("/wechat/binder")
    public AjaxResult binderWechat(@RequestBody LoginDto loginDto){
        try {

            return loginInfoService.binderWechat(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试！！！");

        }
    }



    @GetMapping ("/logout/{token}")
    public AjaxResult logout(@PathVariable("token") String token){
        try {
            loginInfoService.logout(token);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试！！！");

        }
    }
}
